package com.sitechasia.webx.components.xtable.toolbar;

import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.HtmlView;
import org.extremecomponents.table.view.html.CalcBuilder;
import org.extremecomponents.table.view.html.ColumnBuilder;
import org.extremecomponents.table.view.html.TableBuilder;
import org.extremecomponents.util.HtmlBuilder;

import com.sitechasia.webx.components.xtable.customertag.CeRowBuilder;

/**
*
* <p>版权所有：中企动力
* <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
* <p>功能描述：res列表样式的视图
*
* @author kuanglingxuan
* Created on 2007-7-31
*/
public class MyListPageView extends HtmlView{

	private String divWidth;
	private String divHeight;

	private Integer rowDisplayed;
	private Integer defaultDisplayed;

	/**
	 * 功能描述：用于ResRowBuilder类的初始化
	 */
	@Override
	protected void init(HtmlBuilder html, TableModel model)
    {
        setTableBuilder(new TableBuilder(html, model));
        setRowBuilder(new CeRowBuilder(html, model));
        setCalcBuilder(new CalcBuilder(html, model));
        this.divWidth = (String)model.getTableHandler().getTable().getAttribute("divWidth")==null?model.getPreferences().getPreference("table.divWidth"):(String)model.getTableHandler().getTable().getAttribute("divWidth");
        this.divHeight = (String)model.getTableHandler().getTable().getAttribute("divHeight")==null?model.getPreferences().getPreference("table.divHeight"):(String)model.getTableHandler().getTable().getAttribute("divHeight");
        this.rowDisplayed = (Integer)model.getContext().getRequestAttribute("ec_crd")!=null?(Integer)model.getContext().getRequestAttribute("ec_crd"):15;
		this.defaultDisplayed = model.getPreferences().getPreference("table.rowsDisplayed") != null?Integer.parseInt(model.getPreferences().getPreference("table.rowsDisplayed")):15;
    }

	/**
	 * 功能描述：列表数据生成之前调用的方法
	 */
	@Override
	protected void beforeBodyInternal(TableModel model) {
//		根据ui加入table标签以及样式到hearder----- begin



		if(divWidth != null && !"".equals(divWidth))
			getTableBuilder().getHtmlBuilder().div().style("overflow:auto;width:"+ divWidth +";height:"+ divHeight +";background: fff;").close();
		getTableBuilder().getHtmlBuilder().table(2).width("100%").styleClass("gridControl gridArea").cellPadding("0")
				.cellSpacing("0").close();


		getTableBuilder().titleRowSpanColumns();

		this.headerRow(getTableBuilder().getHtmlBuilder(), model);

		getTableBuilder().getHtmlBuilder().tableEnd(2);
		if(divHeight != null && divWidth == null && rowDisplayed > defaultDisplayed){

			getTableBuilder().getHtmlBuilder().div().style("overflow-y:scroll;height:"+ divHeight +";background: fff;").close();
		}else if(model.getTableHandler().getTable().getAttribute("divHeight") != null){
			getTableBuilder().getHtmlBuilder().div().style("overflow-y:scroll;height:"+ divHeight +";background: fff;").close();
		}
//		根据ui加入table标签以及样式到列表hearder----- end

//		根据ui加入table标签以及样式到列表数据body ---- begin
		String tableId = model.getPreferences().getPreference("table.tableId");//model.getTableHandler().getTable().getTableId();默认tableId=ec，所以直接取属性文件值
		if(tableId != null){
			getTableBuilder().getHtmlBuilder().table(2).width("100%").id(tableId);
		}
		String styleClass = model.getTableHandler().getTable().getStyleClass();
		String cellSpacing = model.getTableHandler().getTable().getCellspacing();
		String border = model.getTableHandler().getTable().getBorder();
		String cellpadding = model.getTableHandler().getTable().getCellpadding();

		if(styleClass != null){
			getTableBuilder().getHtmlBuilder().styleClass(styleClass);
		}
		if(cellSpacing != null){
			getTableBuilder().getHtmlBuilder().cellSpacing(cellSpacing);
		}
		if(border != null){
			getTableBuilder().getHtmlBuilder().border(border);
		}
		if(cellpadding != null){
			getTableBuilder().getHtmlBuilder().cellPadding(cellpadding);
		}

		getTableBuilder().getHtmlBuilder().close();


	}
	/**
	 * 功能描述：列表每行数据的生成
	 */
	@Override
	public void body(TableModel model, Column column){

		if(column.isFirstColumn()){
			getRowBuilder().rowStart();

		}


		if(column.getStyleClass() == null){

			StringBuffer cellDisplay = new StringBuffer(column.getCellDisplay());
			Boolean isNumeric = (Boolean) column.getAttribute("isNumeric");
			if(isNumeric != null && isNumeric == true){//在ResColumnTag里面已经都变成字符串，这里判断一下是否数字
//				System.out.println("column.getValue()--Number--->"+column.getValue());
				String className = " class=\"grid align_right\" ";
				cellDisplay.insert(cellDisplay.indexOf("<td")+3, className);
//				cellDisplay = "<td class=\""+className+"\" " + cellDisplay.substring(cellDisplay.indexOf("<td"));

			}else{
				String className = " class=\"grid align_left\" ";
				cellDisplay.insert(cellDisplay.indexOf("<td")+3, className);
//				cellDisplay = "<td class=\""+className+"\" " + cellDisplay.substring(cellDisplay.indexOf("<td"));

//				System.out.println("column.getValue()--String--->"+column.getValue());
			}
//			System.out.println("cellDisplay--->"+cellDisplay);
			column.setCellDisplay(cellDisplay.toString());
		}

        this.getHtmlBuilder().append(column.getCellDisplay());

        if(column.isLastColumn()){
        	getRowBuilder().rowEnd();
        }
	}



	/**
	 * 功能描述：列表数据生成之后调用的方法
	 */
	@Override
	protected void afterBodyInternal(TableModel model) {

		getTableBuilder().getHtmlBuilder().tableEnd(2);
		if(divHeight != null && divWidth == null)
			getTableBuilder().getHtmlBuilder().divEnd();
		if(divWidth != null && !"".equals(divWidth))
			getTableBuilder().getHtmlBuilder().divEnd();
//		根据ui加入table标签以及样式到列表数据body ---- end
		getTableBuilder().getHtmlBuilder().table(0).width("100%").cellPadding("0").border("0").cellSpacing("0").close();
		toolbar(getHtmlBuilder(), getTableModel());//工具条
		getTableBuilder().getHtmlBuilder().tableEnd(0);
//		getTableBuilder().getHtmlBuilder().tdEnd();
//		getTableBuilder().getHtmlBuilder().trEnd(2);
//		getTableBuilder().tableEnd();
//		System.out.println("rowend---->"+model.getLimit().getRowEnd());
	}

	/**
	 * 功能描述：初始化工具条
	 */
	protected void toolbar(HtmlBuilder html, TableModel model) {
		// (new ExToolbar(html, model)).layout();
		(new MyToolBar(html, model)).layout();
	}

//	protected void statusBar(HtmlBuilder html, TableModel model) {
//		// (new ExToolbar(html, model)).layout();
//		(new MyToolBar(html, model)).layout();
//	}
	public void headerRow(HtmlBuilder html, TableModel model)
    {


        html.tr(1).close();
        List columns = model.getColumnHandler().getHeaderColumns();
        Column column;
        for(Iterator iter = columns.iterator(); iter.hasNext(); html.append(column.getCellDisplay()))
            column = (Column)iter.next();
        if(divHeight != null && divWidth == null && rowDisplayed > defaultDisplayed){
        	html.td(3).width("15px").close();
        	html.tdEnd();
		}
        html.trEnd(1);
    }
}
